package com.example.eventpush.listener;

import com.example.eventpush.event.StudentEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ZhenWuWang
 */
@Component
public class StudentListener
{
    private AtomicInteger count = new AtomicInteger(0);

    private ConcurrentHashMap<Double, StudentEvent> map = new ConcurrentHashMap<>();

    @EventListener(condition = "#studentEvent.level == T(com.example.eventpush.event.Level).PASS")
    public void handleEvent(StudentEvent studentEvent) throws Exception
    {
//        System.out.println("handle start");
        System.out.println(Thread.currentThread().getName() + "  " + (count.incrementAndGet()) + "  " + studentEvent.getUserName() + "  " + studentEvent.getScore());
//        System.out.println("handle end");
//        map.put(studentEvent.getScore(), studentEvent);
//        if (count.incrementAndGet() == 1000)
//        {
//
//            TimeUnit.SECONDS.sleep(2);
//
//            System.out.println("------------------------------------------------");
//            List<Double> doubleList = new ArrayList<>();
//            for (int i = 0; i < 1000; i++)
//            {
//                doubleList.add(i * 1d);
//            }
//
//            map.keySet().stream().sorted(Double::compareTo).forEach(key ->
//            {
//                StudentEvent event = map.get(key);
//                System.out.println(event.getUserName() + "  " + event.getScore() + "  " +
//                        (doubleList.get(event.getScore().intValue()).doubleValue() == event.getScore().doubleValue()));
//            });
//        }
    }
}
